package com.mxpio.erp.equipment.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepPlanTarget;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanTargetService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentUpkeepPlanTargetController}", description = "保养计划目标")
@RestController
@RequestMapping("/erp/equipment/upkeep/plan/target/")
public class EquipmentUpkeepPlanTargetController {

    @Autowired
    private EquipmentUpkeepPlanTargetService equipmentUpkeepPlanTargetService;


    @GetMapping("list")
    @Operation(summary = "保养计划目标列表", description = "获取保养计划目标列表（无分页）", method = "GET")
    public Result<List<EquipmentUpkeepPlanTarget>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentUpkeepPlanTarget> list = equipmentUpkeepPlanTargetService.list(EquipmentUpkeepPlanTarget.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "保养计划目标列表", description = "获取保养计划目标列表（分页）", method = "GET")
    public Result<Page<EquipmentUpkeepPlanTarget>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentUpkeepPlanTarget> pageResult = equipmentUpkeepPlanTargetService.listPage(EquipmentUpkeepPlanTarget.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取保养计划目标列表", description = "根据ID获取保养计划目标列表", method = "GET")
    public Result<EquipmentUpkeepPlanTarget> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentUpkeepPlanTarget equipmentUpkeepPlanTarget = equipmentUpkeepPlanTargetService.getById(EquipmentUpkeepPlanTarget.class, id);
        return Result.OK(equipmentUpkeepPlanTarget);
    }

    @RequestMapping("add")
    @Operation(summary = "保存保养计划目标", description = "保存保养计划目标")
    public Result<?> add(@RequestBody List<EquipmentUpkeepPlanTarget> targetList) throws MBootException {
        if (CollectionUtil.isEmpty(targetList)){
            return Result.OK("目标为空！");
        }
        EquipmentUpkeepPlanTarget equipmentUpkeepPlanTarget = targetList.get(0);
        // 新增区域目标
        if (EquipmentEnums.UpkeepPlanTarget.AREA_TARGET.getCode().equals(equipmentUpkeepPlanTarget.getCheckType())) {
            return equipmentUpkeepPlanTargetService.addAreaTarget(equipmentUpkeepPlanTarget);
        } else {
            // 新增设备目标
            return equipmentUpkeepPlanTargetService.addEqpTargets(targetList);
        }
    }

    @RequestMapping("addByCategory")
    @Operation(summary = "保存保养计划目标-根据分类添加", description = "保存保养计划目标-根据分类添加")
    public Result<?> addByCategory(@RequestBody EquipmentUpkeepPlanTarget planTarget) throws MBootException {
        return equipmentUpkeepPlanTargetService.addTargetByCategoryId(planTarget.getPlanId(), planTarget.getCategoryId());
    }


    @PutMapping("edit")
    @Operation(summary = "编辑保养计划目标", description = "编辑保养计划目标", method = "PUT")
    public Result<EquipmentUpkeepPlanTarget> edit(@RequestBody EquipmentUpkeepPlanTarget equipmentUpkeepPlanTarget) {
        equipmentUpkeepPlanTargetService.update(equipmentUpkeepPlanTarget);
        return Result.OK(equipmentUpkeepPlanTarget);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除保养计划目标", description = "删除保养计划目标,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentUpkeepPlanTarget> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for (String key : id) {
            equipmentUpkeepPlanTargetService.delete(EquipmentUpkeepPlanTarget.class, key);
        }
        return Result.OK();
    }


}
